package attack.nartan;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.examples.lights.LightTest;
import org.newdawn.slick.geom.Rectangle;

import framework.AnimationManager;
import framework.BasicFighter;
import framework.Util;
import framework.attack.BasicAttack;
import framework.movements.LinearMovement;

public class SwordStrike extends BasicAttack<BasicFighter> {
	
	protected Animation attackAnimation;
	
	protected Animation attackAnimationBackward;
	
	protected Animation attackAnimationUsed;
	
	protected SpriteSheet spriteSheet;
	
	protected boolean isJumpingAuthorized;
	
	protected final static int FULLY_CHARGED = 1200;
	
	protected final static int MIN_FLICKERING = 100;
	
	protected final static int MAX_FLICKERING = 500;
	
	public SwordStrike(BasicFighter attacker) {
		AnimationManager n = attacker.getAnimations();
		this.spriteSheet = n.getSpriteSheet();
		Image[] frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(0, 2);
		frames[1] = this.spriteSheet.getSprite(1, 2);
		frames[2] = this.spriteSheet.getSprite(1, 2);
		this.attackAnimation = new Animation(frames, 150);
		this.attackAnimation.setDuration(0, 50);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attacker = attacker;
		this.isJumpingAuthorized = false;
	}
	
	@Override
	public void update(GameContainer gc, int delta) {
		if (this.attackAnimationUsed.isStopped()) {
			interrupt();
		}
		super.update(gc, delta);
	}
	
	@Override
	public void draw(Graphics g) {
		BasicFighter a = getAttacker();
		boolean frame = attackAnimationUsed.getFrame() == 1;
		int current = Math.min(FULLY_CHARGED, currentTime);
		int time = Math.max(MIN_FLICKERING, (int) (MAX_FLICKERING - current * ((MAX_FLICKERING - MIN_FLICKERING) / (float)FULLY_CHARGED)));
		System.out.println(time);
		if (currentTime < 300 || frame || (currentTime > 150 && (currentTime / time) % 2 == 0)) {
		    this.attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight());
		} else if(currentTime >= FULLY_CHARGED) {
			this.attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight(), new Color(100, 100, 100, 120));
		} else {
			this.attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight(), new Color(160, 160, 160, 120));
		}
	}
	
	@Override
	public void trigger() {
		if(!this.isJumpingAuthorized && !getAttacker().isJumping() || this.isJumpingAuthorized) {
			super.trigger();
			this.attackAnimation.restart();
			this.attackAnimationBackward.restart();
			this.attackAnimationUsed = getAttacker().isForward() ? this.attackAnimation : this.attackAnimationBackward;
			this.attackAnimationUsed.setAutoUpdate(false);
			this.attackAnimationUsed.setLooping(false);
		}
	}

	@Override
	public int getDuration() {
		return attackAnimationUsed.getFrame() == 1 ? 6000 : 5000;
	}
	
	@Override
	public Rectangle getHitBox() {
		Rectangle hitBox, att;
		att = getAttacker().getHitBox();
		int width = (int) (att.getWidth() * 3);
		if(attackAnimationUsed == attackAnimation) {
			hitBox = new Rectangle(att.getX() +  att.getWidth(), 
					               att.getY() +  att.getHeight() / 3, 
					               width, att.getHeight() / 4);
		} else {
			hitBox = new Rectangle(att.getX() - width, 
					               att.getY() +  att.getHeight() / 3, 
					               width, att.getHeight() / 4);
		}
		return attackAnimationUsed.getFrame() == 1 ? hitBox : null;
	}
	
	@Override
	public boolean isBlocking() {
		return isActive();
	}

	@Override
	public boolean isDrawingAttacker() {
		return isActive();
	}
	
	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		int forward = f.isForward() ? -1 : 1;
		if (!super.hasHit()) {
			LinearMovement m = new LinearMovement(f, (int) (currentTime * 1000 * getScale() / getDuration() + 10) * forward, 0);
			f.move(m);
		}
		super.onHit(f, Collision);
	}

	@Override
	public int getPower() {
		int time = Math.max(FULLY_CHARGED, currentTime);
		int power;
		if (currentTime > FULLY_CHARGED) {
		    power = 40;
		} else {
			power = (currentTime * 20 / time);
		}
		return power;
	}

	@Override
	public void onRelease() {
		if (isActive()) {
			attackAnimationUsed.setAutoUpdate(true);
		}
	}
}
